services:
  {{ service_name }}:
    image: docker.io/gitea/gitea:1.25.1
    container_name: {{ container_name }}
    env_file:
      - .env.gitea
    {% if ports_enabled %}
    ports:
      - {{ ports_http }}:3000
      - {{ ports_ssh }}:22
    {% endif %}
    {% if network_enabled or traefik_enabled %}
    networks:
      {% if network_enabled %}
      - {{ network_name }}
      {% endif %}
      {% if traefik_enabled %}
      - {{ traefik_network }}
      {% endif %}
    {% endif %}
    {% if traefik_enabled %}
    labels:
      - traefik.enable=true
      - traefik.docker.network={{ traefik_network }}
      - traefik.http.services.{{ service_name }}.loadbalancer.server.port=3000
      - traefik.http.services.{{ service_name }}.loadbalancer.server.scheme=http
      - traefik.http.routers.{{ service_name }}-http.rule=Host(`{{ traefik_host }}`)
      - traefik.http.routers.{{ service_name }}-http.entrypoints={{ traefik_entrypoint }}
      {% if traefik_tls_enabled %}
      - traefik.http.routers.{{ service_name }}-https.rule=Host(`{{ traefik_host }}`)
      - traefik.http.routers.{{ service_name }}-https.entrypoints={{ traefik_tls_entrypoint  }}
      - traefik.http.routers.{{ service_name }}-https.tls=true
      - traefik.http.routers.{{ service_name }}-https.tls.certresolver={{ traefik_tls_certresolver }}
      {% endif %}
    {% endif %}
    volumes:
      - gitea-data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      - {{ service_name }}-postgres
    restart: {{ restart_policy }}

  {% if not database_external %}
  {{ service_name }}-postgres:
    image: docker.io/library/postgres:17.7
    container_name: {{ service_name }}-db
    env_file:
      - .env.postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U {{ database_user }}"]
      start_period: 30s
      interval: 10s
      timeout: 10s
      retries: 5
    volumes:
      - gitea-db:/var/lib/postgresql/data
    {% if network_enabled or traefik_enabled %}
    networks:
      {% if network_enabled %}
      - {{ network_name }}
      {% endif %}
      {% if traefik_enabled %}
      - {{ traefik_network }}
      {% endif %}
    {% endif %}
    restart: {{ restart_policy }}
  {% endif %}

volumes:
  gitea-data:
    driver: local
  {% if not database_external %}
  gitea-db:
    driver: local
  {% endif %}

{% if network_enabled or traefik_enabled %}
networks:
  {% if network_enabled %}
  {{ network_name }}:
    {% if network_external %}
    external: true
    {% endif %}
  {% endif %}
  {% if traefik_enabled %}
  {{ traefik_network }}:
    external: true
  {% endif %}
{% endif %}
